package com.wmh.baseservice.operationaid.controller.competitor;

import cn.hutool.core.collection.CollUtil;
import com.wmh.baseservice.common.mybatisplus.structure.LambdaQueryWrapperX;
import com.wmh.baseservice.common.utils.page.pojo.PageResult;
import com.wmh.baseservice.common.utils.page.util.PageUtil;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.operationaid.entity.CompetitorProduct;
import com.wmh.baseservice.operationaid.pojo.vo.competitor.CompetitorListReqVO;
import com.wmh.baseservice.operationaid.pojo.vo.competitor.CompetitorPageReqVO;
import com.wmh.baseservice.operationaid.pojo.vo.competitor.RobotCommitReqVO;
import com.wmh.baseservice.operationaid.service.CompetitorProductService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author 谢骏
 * @date 2023/8/24 11:17
 **/
@Validated
@RestController
@RequestMapping("operationAid/competitorProduct")
@Slf4j
public class CompetitorProductController {

    @Resource
    private CompetitorProductService competitorProductService;

    @ApiOperation("提交竞品数据接口")
    @PostMapping("public/commit")
    public String robotCommit(@Valid @RequestBody RobotCommitReqVO reqVO) {
        competitorProductService.robotCommit(reqVO);
        return ReturnBody.success();
    }

    @ApiOperation("分页接口")
    @GetMapping("page")
    public String page(@Valid CompetitorPageReqVO reqVO) {
        PageResult<CompetitorProduct> pageResult = new PageUtil<CompetitorProduct>(reqVO).startPage((page, wrapper) -> {
            wrapper.lambda()
                    .eq(CompetitorProduct::getDataCycle, reqVO.getDataCycle())
                    .eq(CompetitorProduct::getCategory, reqVO.getCategory());
            competitorProductService.page(page, wrapper);
        });
        return ReturnBody.success(pageResult);
    }

    @ApiOperation("直接获取列表")
    @GetMapping("list")
    public String list(CompetitorListReqVO reqVO) {
        List<CompetitorProduct> allList = competitorProductService.list(new LambdaQueryWrapperX<CompetitorProduct>()
                .eq(CompetitorProduct::getDataCycle, reqVO.getDataCycle())
                .eq(CompetitorProduct::getCategory, reqVO.getCategory()));
        return ReturnBody.success(allList);
    }

    @ApiOperation("获取数据周期选项")
    @GetMapping("dataCycleOptions")
    public String dataCycleOptions(@NotBlank(message = "未提交类目") String category) {
        return ReturnBody.success(
                CollUtil.map(competitorProductService.list(new LambdaQueryWrapperX<CompetitorProduct>()
                        .eq(CompetitorProduct::getCategory, category)
                        .groupBy(CompetitorProduct::getDataCycle)
                        .orderByDesc(CompetitorProduct::getUpdateTime)
                        .select(CompetitorProduct::getDataCycle)), CompetitorProduct::getDataCycle, true)
        );
    }

    @ApiOperation("获取类目选项接口")
    @GetMapping("categoryOptions")
    public String categoryOptions() {
        return ReturnBody.success(
                CollUtil.map(competitorProductService.list(new LambdaQueryWrapperX<CompetitorProduct>()
                        .groupBy(CompetitorProduct::getCategory)
                        .orderByDesc(CompetitorProduct::getUpdateTime)
                        .select(CompetitorProduct::getCategory)), CompetitorProduct::getCategory, true)
        );
    }

}
